from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz

def decision_iris():
    #获取数据
    iris = load_iris()
    #划分数据集
    x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=6)
    #设置预估器
    estimator = DecisionTreeClassifier(criterion="entropy")
    estimator.fit(x_train,y_train)
    #评估模型
    # 1.直接比对真实值和预测值
    y_predict = estimator.predict(x_test)
    print("预测值：\n", y_predict)
    print("直接比对真实值和预测值：\n", y_predict == y_test)
    # 2.准确率
    accuacy = estimator.score(x_test, y_test)
    print("准确率：\n", accuacy)
    #数据可视化
    export_graphviz(estimator,out_file="iris_tree.dot",feature_names=iris.feature_names)
    return None

if __name__ == '__main__':
    decision_iris()